home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-15 | 10.7 KB | 477 lines | [TEXT/CWIE] |
- /*
-
- File: SprocketStarter.cp
- Project: Sample code for Sprocket Framework 1.1 (DR2), released 6/15/96
- Contains: Boilerplate application-specific code
- To Do: Whatever your heart desires
-
- Sprocket Major Contributors:
- ----------------------------
- Dave Falkenburg, producer of Sprocket 1.0
- Bill Hayden, producer of Sprocket 1.1
- Steve Sisak, producer of the upcoming Sprocket 2.0
-
- Pete Alexander Steve Falkenburg Randy Thelen
- Eric Berdahl Nitin Ganatra Chris K. Thomas
- Marshall Clow Dave Hershey Leonard Rosenthal
- Tim Craycroft Dave Mark Dean Yu
- David denBoer Gary Powell
- Cameron Esfahani Jon Summers Apple Computer, Inc.
-
- Comments, Additions, or Corrections:
- ------------------------------------
- Bill Hayden, Nikol Software <nikol@codewell.com>
-
- */
-
-
-
- #include "SprocketStarter.h"
-
- #include "Sprocket.h"
- #include "UDialog.h"
- #include "Window.h"
- #include "StandardMenus.h"
- #include "SplashWindow.h"
- #include "FuturesDemo.h"
-
- #include "SerialNumberProtection.h"
- #include "SpokenCommandHandler.h"
-
- #include <Menus.h>
- #include <Devices.h>
-
- #include "TextWindow.h"
- #include "SampleWindow.h"
-
- #include "PreferencesDialogWindow.h"
- #include "MPPreferencesDialogWindow.h"
-
-
- TSerialNumberProtection* SN;
-
- #if GENERATINGPOWERPC
- TSpokenCommandHandler* SCH;
- #endif
-
- // Function Prototypes:
-
- void AboutBox(void);
- void OpenExistingDocument(void);
- OSErr CreateNewTextWindow(void);
- OSErr CreateNewSampleWindow(void);
-
- Boolean CheckRegistration(void);
-
- #define kAboutBoxFor68K 256
- #define kAboutBoxForPowerPC 257
-
-
- ////////////////////////////////////////////////////////////////////
- //
- // SetupApplication
- //
- // Modify this function to provide any application-specific
- // initialization you may require.
- //
- // This is a good place to call EnterMovies, allocate
- // sound channels, setup network things, etc.
- //
- // For kicks, I am sercuring this application with Serial Number protection
-
- OSErr SetupApplication(void)
- {
- if (!CheckRegistration())
- ; // a normal app would quit here due to incorrect registration, but this is just a sample
-
- #if GENERATINGPOWERPC
- if (gHasSpeechRecognitionManager)
- {
- gSplashWindow->NewStatus("\pLoading Speech Recognition Handler…");
-
- SCH = new TSpokenCommandHandler;
- if (SCH)
- {
- OSErr err;
-
- err = SCH->ISpokenCommandHandler();
- if (noErr == err)
- {
- // Register our spoken commands. Could this be any easier?
-
- SCH->RegisterSpokenCommand("Cut\0", cCut);
- SCH->RegisterSpokenCommand("Copy\0", cCopy);
- SCH->RegisterSpokenCommand("Paste\0", cPaste);
- SCH->RegisterSpokenCommand("Clear\0", cClear);
- SCH->RegisterSpokenCommand("Undo\0", cUndo);
- SCH->RegisterSpokenCommand("Select All\0", cSelectAll);
- SCH->RegisterSpokenCommand("Close\0", cClose);
-
- SCH->RegisterSpokenCommand("Black\0", cBlack);
- SCH->RegisterSpokenCommand("Blue\0", cBlue);
- SCH->RegisterSpokenCommand("Green\0", cGreen);
- SCH->RegisterSpokenCommand("Red\0", cRed);
- SCH->RegisterSpokenCommand("Pink\0", cPink);
- SCH->RegisterSpokenCommand("Orange\0", cOrange);
- SCH->RegisterSpokenCommand("Brown\0", cBrown);
-
- SCH->RegisterSpokenCommand("Plain\0", cPlainText);
- SCH->RegisterSpokenCommand("Bold\0", cBold);
- SCH->RegisterSpokenCommand("Italic\0", cItalic);
- SCH->RegisterSpokenCommand("Underline\0", cUnderline);
-
- err = SCH->RegisterSpokenCommand("Save\0", cSave);
-
- if (!err)
- err = SCH->StartListening();
-
- if (err)
- ErrorReporter(err, __FILE__, __LINE__);
-
- gSplashWindow->NewStatus("\pSpeech Recognition Handler Loaded");
- }
- else
- {
- ErrorReporter(err, __FILE__, __LINE__);
- delete SCH;
- }
- }
- }
- #endif
-
- InitCursor();
-
- gSplashWindow->NewStatus("\pLoading Futures Demo…");
- InitFuturesDemo();
-
- return noErr;
- }
-
-
- ////////////////////////////////////////////////////////////////////
- //
- // CheckRegistration
- //
- // Check to see if the app is registered - this function is not required for Sprocket
-
- Boolean CheckRegistration(void)
- {
- OSErr err;
-
- SN = new TSerialNumberProtection(32000);
-
- if (SN)
- {
- Str32 Serial;
- Str255 Name, Company;
-
- gSplashWindow->NewStatus("\pChecking Registration…");
- err = SN->ReadRegistration(Serial, Name, Company);
- if (err || Name[0] == 0)
- {
- short itemHit = 0;
- DialogRef aDialog = GetNewDialog(259, nil, (WindowRef)-1);
-
- SetDialogDefaultItem(aDialog, 1);
- SetDialogCancelItem(aDialog, 2);
- SetDialogTracksCursor(aDialog, true);
-
- while (itemHit != 1 && itemHit != 2)
- {
- ModalDialog(StandardDialogFilterUPP, &itemHit);
- }
-
- GetEditText(aDialog, 3, Name);
- GetEditText(aDialog, 4, Company);
- GetEditText(aDialog, 5, Serial);
-
- HideWindow(GetDialogWindow(aDialog));
- DisposeDialog(aDialog);
-
- err = SN->WriteRegistration(Serial, Name, Company);
- }
-
- if (err == noErr)
- {
- gSplashWindow->NewStatus("\pRegistering on the network…");
- err = SN->EnableProtection();
- if (err)
- {
- err = SN->ReadRegistration(Serial, Name, Company);
- ParamText("\pThe serial number ", Serial, "\p is already in use, or an AppleTalk error occured.", "\p");
- StandardAlert(128);
- }
- }
- }
-
- if (!SN || err)
- {
- Str255 errStr;
- NumToString(err, errStr);
- ParamText("\pI was unable to verify the serial number of this application due to error ", errStr, "\p.", "\p");
- StandardAlert(128);
- return false;
- }
-
- return true;
- }
-
- ////////////////////////////////////////////////////////////////////
- //
- // CreateNewTextWindow
- //
- // Create a new Text window
-
- OSErr CreateNewTextWindow(void)
- {
- TTextWindow *aNewWindow = new TTextWindow();
-
- if (aNewWindow)
- return aNewWindow->ITextWindow();
- else
- return memFullErr;
- }
-
-
- ////////////////////////////////////////////////////////////////////
- //
- // CreateNewSampleWindow
- //
- // Create a new Sample window
-
- OSErr CreateNewSampleWindow(void)
- {
- TSampleWindow *aNewWindow = new TSampleWindow();
-
- if (!aNewWindow)
- return memFullErr;
-
- return noErr;
- }
-
-
- ////////////////////////////////////////////////////////////////////
- //
- // TearDownApplication
- //
- // Modify this function to tear down anything that you allocated
- // from within SetupApplication.
-
- void TearDownApplication(void)
- {
- CleanupFuturesDemo();
-
- if (SN)
- delete SN;
-
- #if GENERATINGPOWERPC
- if (SCH)
- delete SCH;
- #endif
- }
-
-
- ////////////////////////////////////////////////////////////////////
- //
- // HandleMenuCommand
- //
- // Modify this function to handle any registered menu commands
- // that aren’t specific to a particular window.
-
- void HandleCommand(CommandID theCommand)
- {
- OSErr err;
-
- switch (theCommand)
- {
- case cAbout:
- AboutBox();
- break;
-
- case cNewTextWindow:
- CreateNewTextWindow();
- break;
-
- case cNewSampleWindow:
- CreateNewSampleWindow();
- break;
-
- case cOpen:
- OpenExistingDocument();
- break;
-
- case cPreferences:
- err = gMenuBar->HiliteMenusForModalDialog(false);
- if (err)
- ErrorReporter(err, __FILE__, __LINE__);
-
- TPreferencesDialogWindow * prefsDialog = new TPreferencesDialogWindow;
- break;
-
- case cMPPreferences:
- TMPPreferencesDialogWindow * MPprefsDialog = new TMPPreferencesDialogWindow;
-
- err = gMenuBar->HiliteMenusForModalDialog(false);
- if (err)
- ErrorReporter(err, __FILE__, __LINE__);
-
- if (MPprefsDialog)
- MPprefsDialog->IMultiPanelDialogWindow(1, 2, 3);
- break;
-
- case cPing:
- SendSimpleAEvt(kSillyEventClass, kPing1Event);
- break;
-
- case cPing2:
- SendSimpleAEvt(kSillyEventClass, kPing2Event);
- break;
-
- default:
- break;
- }
- }
-
-
- ////////////////////////////////////////////////////////////////////
- //
- // HandleMenuSelection
- //
- // Modify this function to handle any menu selections that aren’t
- // specific to a particular window. Normally, you shouldn’t need to
- // do anything, unless you hate using MenuCommands.
-
- void HandleMenuSelection(MenuID /* theMenu */, MenuItemID /* theItem */)
- {
- }
-
-
- ////////////////////////////////////////////////////////////////////
- //
- // ReadLocalClipboardFromScrap & WriteLocalClipboardToScrap
- //
- // These functions will be called whenever the user switches into
- // or out of your application.
- //
- // If you keep copy of the clipboard in your own data structures,
- // these functions will allow you to keep it in synch so that the
- // user can cut and paste information between your program and
- // other applications.
- //
- // We don’t actually use the clipboard in SprocketSample, so these
- // functions are empty for now.
-
- void ReadLocalClipboardFromScrap(void)
- {
- }
-
- void WriteLocalClipboardToScrap(void)
- {
- }
-
-
- ////////////////////////////////////////////////////////////////////
- //
- // CreateNewDocument, OpenDocument, PrintDocument, and
- // QuitApplication.
- //
- // These functions will be called whenever one of the required
- // AppleEvents is sent to your application— either when your
- // program is launched, or when the user opens a document from
- // the Finder.
-
- OSErr CreateNewDocument(void)
- {
- return CreateNewTextWindow();
- }
-
-
- OSErr OpenDocument(FSSpec* theDocument, void * /*unused*/)
- {
- TTextWindow *aNewWindow = new TTextWindow();
-
- if (aNewWindow)
- return aNewWindow->ITextWindow(theDocument);
- else
- return memFullErr;
- }
-
-
- OSErr PrintDocument(FSSpec* /* theDocument */, void * /*unused*/)
- {
- // We don’t do printing yet, but you could.
- return errAEEventNotHandled;
- }
-
-
- Boolean QuitApplication(void)
- {
- // Just go ahead and say we’re done quitting…
- // We have no app-specific clean-up or chores to do here, but you might.
-
- return true;
- }
-
-
-
- ////////////////////////////////////////////////////////////////////
- //
- // AboutBox
- //
- // Put up our about box, including the version number. Depending
- // on what version we are running, pick the correct DLOG resource.
- //
-
- void AboutBox(void)
- {
- Handle versionHandle;
- StringPtr shortVersionString;
- short itemHit;
- Str32 Serial;
- Str255 Name, Company;
-
- SN->ReadRegistration(Serial, Name, Company);
-
- versionHandle = GetResource('vers', 1);
- if (versionHandle)
- {
- shortVersionString = (StringPtr) ((char *) *versionHandle + 6);
- ParamText(shortVersionString, Name, Company, Serial);
- ReleaseResource(versionHandle);
- }
-
- #if GENERATING68K
- itemHit = StandardAlert(kAboutBoxFor68K);
- #else
- itemHit = StandardAlert(kAboutBoxForPowerPC);
- #endif
- }
-
-
-
- ////////////////////////////////////////////////////////////////////
- //
- // OpenExistingDocument
- //
- // Use StandardFile to ask the user for a file to open. We use
- // CustomGetFile so that we can properly handle update events
- // in other windows while the dialog is active.
-
- void OpenExistingDocument(void)
- {
- StandardFileReply reply;
- SFTypeList ourTypes;
-
- Point where = { -1, -1 };
-
- HiliteWindowsForModalDialog(false);
- CustomGetFile((FileFilterYDUPP) nil, -1, ourTypes, &reply, 0, where,
- (DlgHookYDUPP) nil, StandardDialogFilterYDUPP, nil, nil, nil);
- HiliteWindowsForModalDialog(true);
-
- if (reply.sfGood)
- {
- OpenDocument(&reply.sfFile, nil);
- }
- }
-